package org.lzy.shop.database.entity

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import org.lzy.shop.cart.CartItem

@Entity(tableName = "cart_items")
data class CartItemEntity(
    @PrimaryKey(autoGenerate = true)
    val id: Int = 0,

    @ColumnInfo(name = "goods_id")
    val goodsId: Int,

    @ColumnInfo(name = "goods_name")
    val goodsName: String,

    @ColumnInfo(name = "goods_main_pic")
    val goodsMainPic: String,

    @ColumnInfo(name = "quantity")
    var quantity: Int = 1,

    @ColumnInfo(name = "is_selected")
    var isSelected: Boolean = false
) {
    // 转换方法：从CartItemEntity到CartItem
    fun toCartItem(specs: List<GoodsSpecEntity>): CartItem {
        return CartItem(
            goodsId = this.goodsId,
            goodsName = this.goodsName,
            goodsMainPic = this.goodsMainPic,
            spec = specs.map { it.toGoodsSpecResponse() },
            quantity = this.quantity,
            isSelected = this.isSelected
        )
    }

    // 从CartItem创建CartItemEntity
    companion object {
        fun fromCartItem(cartItem: CartItem): CartItemEntity {
            return CartItemEntity(
                goodsId = cartItem.goodsId,
                goodsName = cartItem.goodsName,
                goodsMainPic = cartItem.goodsMainPic,
                quantity = cartItem.quantity,
                isSelected = cartItem.isSelected
            )
        }
    }
}